﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Models;

namespace 包覆门.MenxingManager
{
    public partial class MenxingEditing : Form
    {
        public MenxingEditing()
        {
            InitializeComponent();

        }
        public string EditingId { get; set; }
        public bool IsNew { get; set; }
        private MenxingDAL dal = new MenxingDAL();
        Menxing mxx = null;
        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            Menxing mx = new Menxing();
            mx.mxbh = this.tbMxbh.Text;
            mx.kxbh = this.tbkxbh.Text;
            mx.mxsm = this.tbMxsm.Text;
            mx.price = this.tbPrice.Text;
            mx.xbmc=this.tbXbmc.Text;
            foreach (RadioButton rb in gbGlass.Controls) {
                if (rb.Checked == true) {
                    mx.glass = Convert.ToInt32(rb.Tag);
                    break;
                }
            }
            if (rbXbYes.Checked)
            {
                mx.IsSpecial = 1;
                mx.SpecialName = this.tbSpecialName.Text;
                mx.AddPrice = this.tbaddprice.Text;
            }
            else {
                mx.IsSpecial = 0;
            }
            foreach (RadioButton rb in gbrb.Controls) {
                if (rb.Checked == true) {
                    mx.IsRb = Convert.ToInt32(rb.Tag);
                    break;
                }
            }
            if (this.rbYesLimit.Checked)
            {
                mx.LimitCC = 1;
                mx.LimitGao = this.tblimitgao.Text.Trim();
                mx.LimitKuan = this.tblimitkuan.Text.Trim();
            }
            else {
                mx.LimitCC=0;
            }
            if (!ValidateData()) return;
            if (IsNew)
            {
                int rs = dal.SaveData(mx);
                if (rs > 0) 
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
            else
            {
                mx.id = Convert.ToInt32(EditingId);
                int rs = dal.UpdateData(mx);
                if (rs > 0)
                {
                    MessageBox.Show("保存成功");
                    DialogResult = DialogResult.OK;
                }
                else 
                {
                    MessageBox.Show("保存失败");
                    DialogResult = DialogResult.No;
                }
            }
        }
        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
        //验证
        private bool ValidateData() {
            bool flag = true;
            string mxbh = this.tbMxbh.Text;
            string mxsm=this.tbMxsm.Text;
            string kxbh = this.tbkxbh.Text;
            string xbmc=this.tbXbmc.Text;
            string price = this.tbPrice.Text;
            if (string.IsNullOrWhiteSpace(mxbh))
            {
                MessageBox.Show("门型编号不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(mxsm))
            {
                MessageBox.Show("门型说明不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(kxbh))
            {
                MessageBox.Show("框型编号不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(xbmc))
            {
                MessageBox.Show("芯板名称不能为空");
                return false;
            }
            if (string.IsNullOrWhiteSpace(price))
            {
                MessageBox.Show("价格不能为空");
                return false;
            }
            if (this.rbXbYes.Checked) 
            {
                if (string.IsNullOrWhiteSpace(this.tbSpecialName.Text)) 
                {
                    MessageBox.Show("付费芯板不能为空");
                    return false;
                }
                if (string.IsNullOrWhiteSpace(this.tbaddprice.Text)) 
                {
                    MessageBox.Show("付费价格不能为空");
                    return false;
                }
            }
            if (this.rbYesLimit.Checked) {
                if (string.IsNullOrWhiteSpace(this.tblimitkuan.Text)) {
                    MessageBox.Show("限制宽不能为空");
                    return false;
                }
                if (string.IsNullOrWhiteSpace(this.tblimitgao.Text))
                {
                    MessageBox.Show("限制高不能为空");
                    return false;
                }
            }
            return flag;
        }

        private void MenxingEditing_Load(object sender, EventArgs e)
        {
            if (IsNew) 
            { 
                SetState(false); 
            }
            else
            {
                 mxx = dal.GetById(EditingId);
                 this.tbkxbh.Text = mxx.kxbh;
                 this.tbMxbh.Text = mxx.mxbh;
                 this.tbMxsm.Text = mxx.mxsm;
                 this.tbXbmc.Text = mxx.xbmc;
                 this.tbPrice.Text = mxx.price;
                foreach(RadioButton rb in this.gbGlass.Controls){

                    if (Convert.ToInt32(rb.Tag).Equals(mxx.glass))
                    {
                        rb.Checked = true;
                    }
                    else {
                        rb.Checked = false;
                    }
                }
                foreach (RadioButton rb in this.gbrb.Controls)
                {

                    if (Convert.ToInt32(rb.Tag).Equals(mxx.IsRb))
                    {
                        rb.Checked = true;
                    }
                    else
                    {
                        rb.Checked = false;
                    }
                }
                if (mxx.IsSpecial == 1)
                {
                    SetState(true);
                    this.rbXbYes.Checked = true;
                    this.tbSpecialName.Text = mxx.SpecialName;
                    this.tbaddprice.Text = mxx.AddPrice;
                }
                else {
                    SetState(false);
                    this.rbXbNo.Checked = true;
                    //this.tbSpecialName.Text = mxx.SpecialName;
                    //this.tbaddprice.Text = mxx.AddPrice;
                }
                if (mxx.LimitCC == 1)
                {
                    this.rbYesLimit.Checked = true;
                    this.tblimitkuan.Text = mxx.LimitKuan;
                    this.tblimitgao.Text = mxx.LimitGao;
                    SetLimitState(true);
                }
                else {
                    this.rbNoLimit.Checked = true;
                    SetLimitState(false);
                    
                }
                

            }
        }
        //若果是否  那么 付费参数改为只读
        private void tbXbYes_CheckedChanged(object sender, EventArgs e)
        {
            
            if (!this.rbXbYes.Checked)
            {
                this.tbSpecialName.Text = "";
                this.tbaddprice.Text = "";
                SetState(false);
            }
            else {
                SetState(true);
                if (IsNew) { } else {
                    this.tbSpecialName.Text = mxx.SpecialName;
                    this.tbaddprice.Text = mxx.AddPrice;
                }
            }
        }

        public void SetState(bool flag) {
            this.tbaddprice.Enabled = flag;
            this.tbSpecialName.Enabled = flag;
        }
        public void SetLimitState(bool flag)
        {
            this.tblimitgao.Enabled = flag;
            this.tblimitkuan.Enabled = flag;
        }
        //特殊尺寸限制z状态控制
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rbNoLimit.Checked)
            {
                this.tblimitkuan.Text = "";
                this.tblimitgao.Text = "";
                SetLimitState(false);
            }
            else {
                SetLimitState(true);
                if (IsNew) { }
                else
                {
                    this.tblimitgao.Text = mxx.LimitKuan;
                    this.tblimitkuan.Text = mxx.LimitGao;
                }
            }
        }
        //限制数字
        private void tblimitkuan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '.' && this.tblimitkuan.Text.IndexOf(".") != -1)
            {
                e.Handled = true;
            }

            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == '.' || e.KeyChar == 8))
            {
                e.Handled = true;
            }  
        }
        //限制数字输入
        private void tblimitgao_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '.' && this.tblimitgao.Text.IndexOf(".") != -1)
            {
                e.Handled = true;
            }

            if (!((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == '.' || e.KeyChar == 8))
            {
                e.Handled = true;
            }  
        }
    }
}
